﻿#include "precomp.h"
#include "jcurve.h"
#include "qwt_plot_curve.h"
#include "qwt_symbol.h"
#include "qwt_spline.h"
#include "qwt_curve_fitter.h"
#include "jcurvewidget.h"

// - class JCurve -

JCurve::JCurve(const QString &title) :
    QwtPlotCurve(title),
    q_shiftCount(100)
{
    init();
}

void JCurve::setSize(size_t size)
{
    q_points.resize(size);
}

void JCurve::append(const QPointF &point)
{
    q_points.append(point);
    update();
}

void JCurve::append(qreal y)
{
    append(QPointF(q_points.count(), y));
}

void JCurve::append(qreal x, qreal y)
{
    append(QPointF(x, y));
}

void JCurve::setSamples(const QVector<QPointF> &samples)
{
    q_points.clear();
    q_points = samples;
    update();
}

void JCurve::shift(const QPointF &point)
{
    q_points.append(point);
    if (q_points.count() > q_shiftCount) {
        q_points.removeFirst();
    }
    update();
}

void JCurve::shift(qreal y)
{
    q_points.append(QPoint(q_points.count(), y));
    if (q_points.count() > q_shiftCount) {
        for (int i = 1; i < q_points.count(); ++i) {
            q_points[i - 1].setY(q_points[i].y());
        }
        q_points.removeLast();
    }
    update();
}

void JCurve::shift(qreal x, qreal y)
{
    shift(QPointF(x, y));
    update();
}

void JCurve::setLinePen(const QPen &pen)
{
    const_cast<QwtSymbol *>(symbol())->setBrush(QBrush(pen.color()));
    setPen(pen);
}

void JCurve::setColor(const QColor &color)
{
    setSymbol(new QwtSymbol(QwtSymbol::Ellipse, QBrush(color.dark()),
                            QPen(color.light(), 1.0), QSize(5, 5)));
    setLinePen(color);
}

void JCurve::clear()
{
    q_points.clear();
    QwtPlotCurve::setSamples(q_points);
}

void JCurve::update()
{
    setPaintAttribute(QwtPlotCurve::ImageBuffer, q_points.count() > 1000);
    QwtPlotCurve::setSamples(q_points);
}

int JCurve::rtti() const
{
    return Rtti_Curve;
}

void JCurve::setCurveStyle(int curveStyle)
{
    switch (curveStyle) {
    case JCurveWidget::CurveStyle_Dots:
        setCurveAttribute(QwtPlotCurve::Fitted, false);
        setStyle(QwtPlotCurve::NoCurve);
        update();
        break;
    case JCurveWidget::CurveStyle_Lines:
        setCurveAttribute(QwtPlotCurve::Fitted, false);
        setStyle(QwtPlotCurve::Lines);
        update();
        break;
    case JCurveWidget::CurveStyle_Fitted:
        setCurveAttribute(QwtPlotCurve::Fitted, true);
        setStyle(QwtPlotCurve::Lines);
        update();
        break;
    default:
        break;
    }
}

void JCurve::init()
{
    setRenderHint(QwtPlotItem::RenderAntialiased, false);
    setSymbol(new QwtSymbol(QwtSymbol::Rect, QBrush(QColor("gray")),
                            QPen(QColor(200, 200, 200), 1), QSize(5, 5)));
    setStyle(QwtPlotCurve::Dots);
    setCurveAttribute(QwtPlotCurve::Fitted, false);
    setPaintAttribute(QwtPlotCurve::ClipPolygons, true);
    setPaintAttribute(QwtPlotCurve::FilterPoints, true);
    //setPaintAttribute(QwtPlotCurve::MinimizeMemory, false);
    setPaintAttribute(QwtPlotCurve::ImageBuffer, true);

    //QwtSplineCurveFitter *curveFitter = new QwtSplineCurveFitter();
    //curveFitter->setSplineSize(200);
    //setCurveFitter(curveFitter);
}
